package day01;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Iterator;

/**
 * 自己写一个迭代器，每次调用hasnext和next就可以返回一个hero对象
 */
public class HeroIterator implements Iterator<Hero> {
    private BufferedReader buffer;
    private String line;

    public HeroIterator() throws Exception {
        buffer = new BufferedReader(new FileReader("data/Hero.txt"));

    }

    @Override
    public boolean hasNext() {

        try {
            line = buffer.readLine();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return line!=null;
    }

    @Override
    public Hero next() {
        //1,aixi_120::180
        String[] arr = line.split(",");
        //aixi_120::180
        String[] arr1 = arr[1].split("_");
        //120::180
        String[] arr2 = arr1[1].split("::");

        return new Hero(Integer.parseInt(arr[0]),arr1[0],Integer.parseInt(arr2[0]),Integer.parseInt(arr2[1]));
    }

}
